clear all
set more off, perm
set maxvar 120000
set varabbrev off
* ---------------------------------------------- *
global dir 	"`1'"
global Data 	$dir/Data
global Tables 	$dir/Tables
global Figures 	$dir/Figures
global Work 	$dir/Work
global Temp 	$dir/Temp
global Pseudo	$dir/Work/Temp
* ---------------------------------------------- *
import sas $Data/IBES/statsumu_epsus.sas7bdat, clear case(lower)
rename *, lower
compress
keep if index(curcode,"USD")>0 
keep if index(measure,"EPS") 
keep if index(fpi, "1")
tempfile EPS
save "`EPS'"
* ---------------------------------------------------------------- *
import sas $Data/IBES/statsumu_xepsus.sas7bdat, clear case(lower)
rename *, lower
compress
keep if index(curcode,"USD")>0 
keep if index(measure,"DPS") | index(measure,"BPS")
keep if index(fpi, "1")
append using "`EPS'"

keep statpers ticker cusip cname oftic measure meanest medest fpedats
reshape wide meanest medest fpedats, i(statpers ticker cusip cname oftic) j(measure) string

gen   mean_payout=meanestDPS/meanestEPS	if meanestEPS>0 & meanestDPS>=0 
gen median_payout=medestDPS /medestEPS 	if medestEPS >0 & medestDPS >=0 
gstats winsor *payout, cuts(1 99) replace
su *payout, d
* ---------------------------------------------------------------- *
* add permcos
merge m:1 ticker statpers using $Work/link_ibes_crsp2024, keepusing(comnam cname permco permno ticker statpers) 
keep if _merge==3  
drop _merge
* make sure there is only one observation per security/statpers
duplicates tag permco permno statpers, gen(dup)
tabulate dup
drop dup
gen datem=mofd(statpers)
format datem %tm
keep comnam cname permco permno ticker datem *payout
* ---------------------------------------------------------------- *
save $Work/payout, replace 
  
